#ifndef SERVER_H
#define SERVER_H

#include <boost/asio.hpp>
#include "connection_manager.h"
#include "message_processor.h"
#include <memory>
#include <unordered_map>
#include <functional>

class Server {
public:
    Server(boost::asio::io_context& io_context, unsigned short port);
    ~Server();

    // 注册消息处理函数
    void register_message_handler(uint32_t msg_type, std::function<void(const std::string&, std::shared_ptr<Connection>)> handler);
    void register_message_processor(MessageProcessor& processor);

private:
    void start_accept();
    void handle_accept(std::shared_ptr<Connection> new_connection, const boost::system::error_code& error);

    boost::asio::io_context& io_context_;
    boost::asio::ip::tcp::acceptor acceptor_;
    ConnectionManager connection_manager_;
    std::unordered_map<uint32_t, std::function<void(const std::string&, std::shared_ptr<Connection>)>> message_handlers_;
    MessageProcessor* message_processor_;
};

#endif